package com.rt.tmpt.service.impl;

import com.googlecode.genericdao.search.Search;
import com.inspiresoftware.lib.dto.geda.assembler.DTOAssembler;
import com.rt.tmpt.bean.A1LogType;
import com.rt.tmpt.bean.A1Operationlog;
import com.rt.tmpt.dao.intf.IA1LogTypeDao;
import com.rt.tmpt.dto.A1LogTypeDto;
import com.rt.tmpt.dto.PaginationDto;
import com.rt.tmpt.dao.intf.IA1OperationlogDao;
import com.rt.tmpt.dto.A1OperationlogDto;
import com.rt.tmpt.dto.Result;
import com.rt.tmpt.others.pageUtils.PaginationService;
import com.rt.tmpt.service.IOperationlogService;
import com.rt.tmpt.utils.DateTool;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Transactional(propagation=Propagation.REQUIRED)
public class OperationlogService implements IOperationlogService {
	@Resource private IA1OperationlogDao operationlogDao;
	@Resource private IA1LogTypeDao logTypeDao;

	public Result addOperationlog(A1OperationlogDto dto)
	{ 
		Result result = new Result();
		if(dto == null) result.fault("无法添加日志");
		if(dto.getLoginName()== null){
			dto.setLoginName("");
			//return result.fault("无登录名");
		} 
		
		//Search search = new Search();
		if(dto.getLogTypeName() == null || dto.getLogTypeName().trim() == "") 
			return result.fault("无操作类型名");
		
		//search.addFilterEqual("logTypeName", dto.getLogTypeName());
		A1LogType a1LogType = new A1LogType();
		a1LogType.setLogTypeName(dto.getLogTypeName());
		Optional<A1LogType> optional = logTypeDao.findOne(Example.of(a1LogType));
		A1LogType logType = null;
		if(!optional.isPresent()){
			logType = new A1LogType();
			logType.logTypeName=dto.getLogTypeName();
			logTypeDao.save(logType);
		}else{
			logType = optional.get();
		}

		dto.setCreateDate(DateTool.getCurTimestamp());
		A1Operationlog operationlog = new A1Operationlog();
		DTOAssembler assembler = DTOAssembler.newAssembler(A1OperationlogDto.class);
		assembler.assembleEntity(dto, operationlog, null, null);

		operationlog.a1LogType=logType;
		
		if(operationlog.description.length()>100){
			operationlog.description = operationlog.description.substring(0,100);
		}
		
		operationlogDao.save(operationlog);
		return result.success();
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
//		System.out.println("中国人".getBytes().length);
//		System.out.println("中国人1".getBytes("utf-8").length);
	}
	
	public PaginationDto<A1OperationlogDto> paginationBySearch(Search search) {
		try {
	    	PaginationService<A1OperationlogDto> paginationService = new PaginationService<A1OperationlogDto>(search);
			/*if(!paginationService.validataPageSize()){return null;};
			SearchResult<A1Operationlog> searchResult =  operationlogDao.searchAndCount(search);
			List<A1Operationlog> entityList = searchResult.getResult();
			int totalCount = searchResult.getTotalCount();
			final List<A1OperationlogDto> dtoList = new ArrayList<A1OperationlogDto>();
			DTOAssembler assembler = DTOAssembler.newAssembler(A1OperationlogDto.class);
			assembler.assembleDtos(dtoList, entityList, null, null);
			paginationService.assemblePaginationDto(dtoList, totalCount);*/
			return paginationService.getPaginationDto();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public List<A1OperationlogDto> queryLogDtoListBySearch(Search search) {
		if(search==null)return null;
		List<A1OperationlogDto> dtoList = new ArrayList<A1OperationlogDto>();
		/*try {
			List<A1Operationlog> entityList = operationlogDao.search(search);
			DTOAssembler assembler = DTOAssembler.newAssembler(A1OperationlogDto.class);
			assembler.assembleDtos(dtoList, entityList, null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		return dtoList;
	}
	
	public List<A1LogTypeDto> queryLogTypeAll() {
		List<A1LogType> logTypeList = logTypeDao.findAll();
		final List<A1LogTypeDto> logTypeDtoList = new ArrayList<A1LogTypeDto>();
		DTOAssembler ass = DTOAssembler.newAssembler(A1LogTypeDto.class);
		ass.assembleDtos(logTypeDtoList, logTypeList, null, null);
		return logTypeDtoList;
	}
}
